import 'dart:convert';

import 'package:excel_utils/pages/week/bean/QuotationRecordBean.dart';
import 'package:excel_utils/repository/constant.dart';
import 'package:excel_utils/service/DioManager.dart';

abstract class IQuotationRecordRepository {
  Future<List<QuotationRecordBean>> getQuotationRecordData({maps});

  Future<List<QuotationRecordBean>> saveQuotationRecordData({required List<QuotationRecordBean> quotationRecordData});

  Future<String> deleteQuotationRecordData({required String quotationRecordId});
}

class QuotationRecordRepository implements IQuotationRecordRepository {
  DioManager dioManager = DioManager();

  @override
  Future<List<QuotationRecordBean>> getQuotationRecordData({maps}) async {
    final response = await dioManager.get(path: "${Constant.QuotationRecordDataUrl}", queryParameters: maps);
    List<QuotationRecordBean> data = [];
    dynamic result = json.decode(response.toString());
    if (result["code"] == 0) {
      result['data'].forEach((v) {
        data.add(QuotationRecordBean.fromJson(v));
      });
      return data;
    } else {
      return [];
    }
  }

  @override
  Future<List<QuotationRecordBean>> saveQuotationRecordData({required List<QuotationRecordBean> quotationRecordData}) async {
    String jsonData = json.encode(quotationRecordData);
    final response = await dioManager.post(path: "${Constant.QuotationRecordAddUrl}", data: jsonData);
    List<QuotationRecordBean> data = [];
    dynamic result = json.decode(response.toString());
    if (result["code"] == 0) {
      result['data'].forEach((v) {
        data.add(QuotationRecordBean.fromJson(v));
      });
      return data;
    } else {
      return [];
    }
  }

  @override
  Future<String> deleteQuotationRecordData({required String quotationRecordId}) async {
    final response = await dioManager.get(path: "${Constant.QuotationRecordDeleteUrl}?quotationRecordId=$quotationRecordId");
    dynamic result = json.decode(response.toString());
    if (result["code"] == 0) {
      return result["data"];
    } else {
      return "-1";
    }
  }
}
